<?php
namespace TestAdmin\Controller;
use Think\Controller;
class SearchReportResultController extends Controller {

    public function getSearchOptions(){

    }

    public function getQuizInfo()
    {
        $data = M("quiz_info")->field("quiz_id, quiz_name, quiz_pagetitle")->select();

        foreach($data as $quiz_info){
            $info = M("element_info")->where(['quiz_id' => $quiz_info['quiz_id']])->field("element_name, element_label")->select();
            $rs['quiz_list'][] = $quiz_info['quiz_name'];
            $info_list[$quiz_info['quiz_name']] = $info;
            $rs['val'] = $info_list;
        }
       // var_dump($rs['quiz_list']);
        $this->ajaxReturn($rs);
       // var_dump($data);
    }



    public function showReport($quiz_name, $uid)
    {

        if($quiz_name == 'wippf_demo'){
            $ctl = A("Quiz/Wippf");
            $ctl->show_wippf($uid);
        }
        else if($quiz_name == '16pf_test'){
            $ctl = A("Quiz/Pf16");
            $ctl->show_16pf($uid);
        }

        else{
            $ctl = A("Quiz/CustomQuiz");

            $ctl->show_report(convertToQuizId($quiz_name) , $uid);
        }

    }

    public function searchReport()
    {

        $search_value = I_withoutReplace("search_value");

        $search_value = json_decode($search_value,true);

        $quiz_name = array_keys($search_value);
        $quiz_name = $quiz_name[0];

       $uid_list = M()->query("select users.uid, users.username, users.email from users, calc_result where users.uid = calc_result.user_id and users.oid = 0 GROUP BY users.uid; ");

        $tvalue_list = M('calc_result');
        $rs = [];
        $i = 0;

        $quiz_info = M("quiz_info")->where(['quiz_name' => $quiz_name])->find();
        $quiz_id = $quiz_info['quiz_id'];
        foreach($uid_list as $uid){
            $data = $tvalue_list->where(['user_id' => $uid['uid'], 'quiz_id' => $quiz_id])->select();


            $userinfo = M('users')->where(['uid' => $uid['uid']])->find();


            foreach($data as &$d){
                if ($d['trans_score'] <=0 )
                    $d['trans_score'] = $d['row_score'];//没有转换分则显示原始分
            }

            if ($data){
                $rs[$i]['score_data'] = $data;

                $rs[$i]['username'] =$userinfo['username'];

                $rs[$i]['age'] = $userinfo['age'];

                $rs[$i]['uid'] = $userinfo['uid'];

                $rs[$i]['sex'] = convertSex($userinfo['sex']) ;

                $i ++;
            }
        }

        //var_dump($rs[0]);

        $userorgdata = $rs;

        $warnvalue = $search_value[$quiz_name];

        $section = [];
        //获取键值
        for ($i = 0; $i < count($warnvalue); $i++){
            $key = array_keys(array_slice($warnvalue[$i], 0, 1));
            $key = $key[0];
            $section[$i] = $key;
        }
        $i = 0;

        foreach($userorgdata as &$u){//$u为每一个用户

            if (!$warnvalue){//没有设定预警值的情况
                foreach($u['score_data'] as &$score){
                    $score['status'] = 0;
                }
                $u['warn'] = 0;
            }
            else{
                foreach($warnvalue as $w){
                    $choosen = $section[$i];
                    if ((int)$w[$choosen]){

                        //判断使用原始分还是转换分（转换分是0或-1使用原始分）

                        switch ($w['select']){//操作符设定
                            case 0:{//小于
                                if ((int)$u['score_data'][$i]['trans_score'] < (int)$w[$choosen]){
                                    $u['score_data'][$i]['status'] = 1;
                                    $u['warn'] = 1;
                                }
                                else{
                                    $u['score_data'][$i]['status'] = 0;//0正常
                                }
                                break;
                            }

                            case 1:{//大于
                                if ((int)$u['score_data'][$i]['trans_score'] > (int)$w[$choosen]){
                                    $u['score_data'][$i]['status'] = 1;
                                    $u['warn'] = 1;
                                }
                                else{
                                    $u['score_data'][$i]['status'] = 0;//0正常
                                }
                                break;
                            }

                        }
                        $i ++;
                    }
                    else{
                        //不作判断
                        //$u['warn'] = 0;
                        $u['score_data'][$i]['status'] = 0;//0正常
                        $i ++;
                    }
                }

            }

            $i = 0;

        }
        $i=0;
        $result = [];
        foreach($userorgdata as $user){

            if ($user['warn'] == 1){
                $result[$i]['uid'] = $user['uid'];
                $result[$i]['username'] = $user['username'];
                $result[$i]['sex'] = $user['sex'];
                $result[$i]['email'] = $user['email'];
                $result[$i++]['quiz_name'] = $quiz_info['quiz_pagetitle'];
            }


        }

        //返回标识后的数据
        //return $result;
        $this->ajaxReturn($result);

    }
}